<?php 
/*
 * module gallery
 * 
 */
 
 class gallery{
 	
	protected $_db;
	function __construct(){
		$this->_db = db::getInstance();
		$this->v = new view();
		$this->v->init(TRUE);
	}
	/****************************************controller ********************************************************/
	/*
	 * list posts
	 */ 
	public function index()
	{
		$page = isset($_REQUEST['page'])?$_REQUEST['page']:'0';
		$list = $this->gallery_list($page);
		$data = array();
		$data['list'] = $list;		
		$this->v->load('header');
		$this->v->load('gallery_list', $data);
		$this->v->load('footer');
		
	}
	
	/*
	 * add post
	 */
	public function add()
	{
		//添加文章          
		if(empty($_POST)){
			$data = array();
			$cat_list = get_category_list();
			$cat_list = cat_options(0, $cat_list);
			$data['cat_list'] = $cat_list;
			$data['action'] = 'add';		
			$this->v->load('header');
			$this->v->load('gallery_edit', $data);
			$this->v->load('footer');
		
		}else{
			require('../lib/uploads.php');	
			$post = $_POST;	
			$data = array();
			if(empty($post['title'])){
				echo 'title can not empty';
				return FALSE;
			}			
			$data['title'] = $post['title'];
			if(empty($post['category'])){
				echo 'category can not empty';
				return FALSE;
			}
			$data['category'] = $post['category'];
			
			$data['des'] = empty($post['des'])?' ':$post['des'];
			$data['author'] = '0';
			$data['status'] = 0;
			$data['date_time'] = date('Y-m-d H:i:s');
			$upload = new uploads();
			$pic = $upload->upload_file('pic');

			if(isset($pic[0]))
			{
				load_lib('images');
				$src = $pic[0]['file_name'];
				$img = new images();	
				$image = $img->resize(ROOTPATH.$src);
				$data['image'] = str_replace(ROOTPATH, '', $image);
			}else{
				$data['image'] = '';
			}
			$ret = $this->_db->insert('gallery', $data);
			if($ret === FALSE){
				echo 'gallery save error';
				$this->_db->error();
				return FALSE;
			}
			$mid = $this->_db->insert_id();
			foreach($pic as $row){
				$p = array();
				$p['gid'] = $mid;
				$p['pic_path'] = $row['file_name'];
				$p['des'] = '';
				$this->_db->insert('pics', $p);								
			}			
			redirect('spider/spider.php?mod=gallery');
		}
	}
	/*
	 * edit post
	 */
	public function edit()
	{
		if(empty($_POST))
		{
			if(!isset($_GET['id'])){
				redirect('spider/spider.php?mod=posts');
				return FALSE;
			}
			$id = intval($_GET['id']);
			$p = $this->post_info($id);
	
			if($p == FALSE){
				redirect('spider/spider.php?mod=posts');
				return FALSE;
			}
			$cat_list = get_category_list();
			$cat_list = cat_options(0, $cat_list);
			
			$data = array();
			$data['action'] = 'edit';
			$data['p'] = $p;
			$data['cat_list'] = $cat_list;
			$this->v->load('header');
			$this->v->load('post_edit', $data);
			$this->v->load('footer');
		}else{
			$post = $_POST;
			$data = array();
			if(empty($post['title'])){
				echo 'title can not empty';
				return FALSE;
			}			
			$data['title'] = $post['title'];
			if(empty($post['category'])){
				echo 'category can not empty';
				return FALSE;
			}
			$data['category'] = $post['category'];
			if(empty($post['content'])){
				echo 'content can not empty';
				return FALSE;
			}
			$data['content'] = $post['content'];
			$data['source'] = !empty($post['source'])?$post['source']:'本站';
			$data['author'] = '0';
			$data['post_date'] = date('Y-m-d H:i:s');
			if($this->save($data, $post['id']) == FALSE){
				echo 'post save error';
			}
			redirect('spider/spider.php?mod=posts');
		}
	}
	
	public function batch_add()
	{
		//添加文章          
		if(empty($_POST)){
			$data = array();
			$cat_list = get_category_list();
			$cat_list = cat_options(0, $cat_list);
			$data['cat_list'] = $cat_list;
			$data['action'] = 'add';		
			$this->v->load('header');
			$this->v->load('gallery_batch_edit', $data);
			$this->v->load('footer');
		
		}else{
			$post = $_POST;	
			$data = array();
			if(empty($post['title'])){
				echo 'title can not empty';
				return FALSE;
			}			
			$data['title'] = $post['title'];
			if(empty($post['category'])){
				echo 'category can not empty';
				return FALSE;
			}
            if(empty($post['num'])){
                echo 'pic can not empty';
                return FALSE;
            }
			$data['category'] = $post['category'];
			
			$data['des'] = empty($post['des'])?' ':$post['des'];
			$data['author'] = '0';
			$data['status'] = 0;
			$data['date_time'] = date('Y-m-d H:i:s');
			$url = $post['url'];
            $num = $post['num'];
			load_plugin('baidu_bar');
			$baidu = new baidu_bar();
			$pic = $baidu->batch_save_pic($url, '/upload/', $num);
			
			if(isset($pic[0]))
			{
				load_lib('images');
				$src = $pic[0];
				$img = new images();	
				$image = $img->resize(ROOTPATH.$src,0,0,0,180,200);
				$data['image'] = str_replace(ROOTPATH, '', $image);
			}else{
				$data['image'] = '';
			}
			$ret = $this->_db->insert('gallery', $data);
			if($ret === FALSE){
				echo 'gallery save error';
				$this->_db->error();
				return FALSE;
			}
			$mid = $this->_db->insert_id();
			foreach($pic as $row){
				$p = array();
				$p['gid'] = $mid;
				$p['pic_path'] = $row;
				$p['des'] = '';
				$this->_db->insert('pics', $p);								
			}			
			redirect('spider/spider.php?mod=gallery');
		}
	}
	/*
	 * del post
	 */ 
	public function del()
	{
		$id = intval($_GET['id']);
        $sql = "delete from gallery where id='{$id}'";
        $this->_db->query($sql);
        $sql = "delete from pics where gid='{$id}'";
        $this->_db->query($sql);
        header("location:".$_SERVER['HTTP_REFERER']);
	}
	    
	/*********************************     model ******************************/
	protected function save($data,$id=NULL){
		if($id == NULL){
			return $this->_db->insert('posts', $data);
		}else{
			if(!is_array($data)){
				return FALSE;
			}
			$sql = "UPDATE posts set ";
			foreach($data as $key=>$val){
				$sql .= $key."='".$val."' ,";
			}
			$sql = rtrim($sql,',');
			$sql .=" where id='$id'";	
			return $this->_db->query($sql);
		}
		
	}
	
	protected function post_info($id){
		$sql = "SELECT * FROM posts WHERE id='$id'";
		$post = $this->_db->query($sql)->result_array();
		if($post == FALSE){
			return FALSE;
		}
		return $post[0];
	}
	
	protected function gallery_list($page, $size=15){
		$sql = "SELECT p.*,c.cat_name FROM gallery as p JOIN category as c ON p.category=c.cid WHERE p.status=0";
		$sql .= " ORDER BY p.id DESC LIMIT ".$page*$size.",".$size;	
		$list = $this->_db->query($sql)->result_array();
		return $list;
	}
 }
